package com.paas.form.core.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.gencode.util.CodeBuildNew;
import com.paas.common.util.StringUtil;
import com.paas.form.core.element.field.Config;
import com.paas.form.core.element.field.FieLdsModel;
import com.paas.form.core.element.field.Option;
import com.paas.form.core.element.field.Slot;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@EqualsAndHashCode(callSuper = true)
@Data
public class ElSelect extends ElementUI {

    private String _placeholder;

    private String _v_model;

    private String css_class = "flex-wrap";

    private String css_style;

    private String event_click;

    private List<Option> option;


    public ElSelect(String prefixName, FieLdsModel fieLdsModel, int indentNum) {
        //
        Config config = fieLdsModel.getConfig();
        //
        Slot solt = fieLdsModel.getSchemeSlot();
        //
//        this.option = solt.getOptions();
        this.option = JSON.parseArray(solt.getOptions(),Option.class);

        this._placeholder = fieLdsModel.getPlaceholder();
        this._v_model = StringUtil.isNotEmpty(prefixName) ? prefixName + "." + fieLdsModel.getVModel() : fieLdsModel.getVModel();
        //
        this.indentNum = indentNum;
    }


    @Override
    public void getElHtml(StringBuilder str) {

    }

    public static final String VM_FORM_SELECT_HTMl = "<el-select v-model=\"${formDataModel.formModel}.${v_model}\" placeholder=\"${placeholder}\" style=\"${css_style}\" class=\"${css_class}\" @click=\"${event_click}\">";

    @Override
    public void getStartTagElHtml(StringBuilder str) {
        //
//        ExpressionParser parser = new SpelExpressionParser();
//        TemplateParserContext parserContext = new TemplateParserContext("${", "}");
        //
        Map<String, Object> params = new HashMap<>();
        params.put("v_model", StringUtil.isNotEmpty(this._v_model) ?  _v_model  : " ");
        params.put("placeholder", StringUtil.isNotEmpty(this._placeholder) ?  this._placeholder  : " ");
        params.put("css_style", StringUtil.isNotEmpty(this.css_style) ?  this.css_style  : " ");
        params.put("css_class", StringUtil.isNotEmpty(this.css_class) ?  this.css_class  : " ");
        params.put("event_click", StringUtil.isNotEmpty(this.event_click) ?  this.event_click  : " ");
        //
        StringBuilder stringBuilder = new StringBuilder();
        //
        String codeTemplate = String.join("", Collections.nCopies(indentNum, "\t")) + VM_FORM_SELECT_HTMl +"\n";
        stringBuilder.append(codeTemplate);
        //
        for (Option op : option) {
            Map<String, Object> opParams = new HashMap<>();
            opParams.put("label", StringUtil.isNotEmpty(op.getFullName()) ? op.getFullName() : " ");
            opParams.put("value", StringUtil.isNotEmpty(op.getEnCode()) ? op.getEnCode() : " ");
            //
            String opTemplate = String.join("", Collections.nCopies(indentNum + 1, "\t")) + "<el-option :label=\"${label}\" :value=\"${value}\"\n";
            stringBuilder.append(CodeBuildNew.getCodeString(opParams,opTemplate));
        }
        //
        String elsl = stringBuilder.substring(0, stringBuilder.lastIndexOf("\n"));
        //
//        str.append(parser.parseExpression(elsl, parserContext).getValue(params, String.class));
        str.append(CodeBuildNew.getCodeString(params,elsl));
        str.append("\n");
    }

    @Override
    public void getEndTagElHtml(StringBuilder str) {
        String codeTemplate = String.join("", Collections.nCopies(indentNum, "\t")) + "</el-select>";
        str.append(codeTemplate);
        str.append("\n");
    }


}